import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static java.awt.BorderLayout.*;
public class WindowBox extends JFrame implements ItemListener {
   JCheckBox [] box;
   JTextArea text;
   JPanel pNorth;
   WindowBox() {
     init();
     setBounds(120,100,260,270);
     setVisible(true);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
   } 
   void init() {
     String [] name={"苹果","海棠","草莓","香蕉","西瓜"};
     box=new JCheckBox[name.length];
     pNorth=new JPanel();
     for(int k=0;k<box.length;k++) {
        box[k]=new JCheckBox(name[k]);
        pNorth.add(box[k]); 
        box[k].addItemListener(this);  //注册当前窗口为监视器
     }
     add(pNorth,NORTH);
     text=new JTextArea();
     add(new JScrollPane(text),CENTER);
   }
   public void itemStateChanged(ItemEvent e) {
     text.setText(null);
     for(int k=0;k<box.length;k++) {
        if(box[k].isSelected())
          text.append("\n"+box[k].getText()) ;
     }
   }
}
